﻿using System.IO;
using System.Linq;
using UnityEditor;
using UnityEngine;


namespace UFrame
{
    public class EffectInfoWindow : SkillSecondWindow
    {
        public SkillEffectData selectEffectData = null;
        public CameraShakeInfo selectCameraData = null;

        private UnityEngine.Object tempObject = null;
        private int bounsIndex = 0;

        private string[] typeName = { "固定型", "追踪型", "地面型", "运动型" };
        private string[] typeHitName = { "在自己身上出现", "在敌人身上出现" };
        private string[] typeCameraMotion = { "抖动", "推进", "拉远", "还原缩放" };

        public EffectInfoWindow(EffectEditor t, float x, float y, float width, float height) : base(t, x, y, width, height)
        {

        }

        public override void OnEditorEvent(IEditorEvent e)
        {
            switch ((SkillEffectEventEnum)e.GetEventID())
            {
                case SkillEffectEventEnum.EffectSelected:
                    {
                        selectCameraData = null;
                        selectEffectData = (SkillEffectData)e.GetEventData();
                        tempObject = null;
                        bounsIndex = 0;

                        if (!string.IsNullOrEmpty(selectEffectData.effectName))
                        {
                            string[] deps = AssetDatabase.GetAssetPathsFromAssetBundle("effect");
                            foreach (var d in deps)
                            {
                                var name = Path.GetFileName(d).Split('.')[0];
                                if (name == (selectEffectData.effectName))
                                {
                                    tempObject = AssetDatabase.LoadAssetAtPath(d, typeof(GameObject));
                                }

                            }
                        }

                        if (!string.IsNullOrEmpty(selectEffectData.boneName))
                        {
                            bounsIndex = root.effectBones.Keys.ToList().FindIndex(0, root.effectBones.Count, (x) =>
                            {
                                return x == selectEffectData.boneName;
                            });
                        }

                        GUI.FocusControl("");
                        break;
                    }
                case SkillEffectEventEnum.CameraShakeSelected:
                    {
                        selectEffectData = null;
                        selectCameraData = (CameraShakeInfo)e.GetEventData();
                        break;
                    }
            }
        }

        public void Clear()
        {
            selectEffectData = null;
            selectCameraData = null;
        }

        protected override void Draw(int id)
        {
            #region effectData
            if (selectEffectData != null)
            {
                GUILayout.Label("特效:");


                GUILayout.BeginHorizontal();
                GUILayout.Label("特效对象:");
                EditorGUI.BeginChangeCheck();
                tempObject = EditorGUILayout.ObjectField(tempObject, typeof(GameObject), false);
                if (EditorGUI.EndChangeCheck())
                {
                    if (tempObject != null)
                    {
                        string path = AssetDatabase.GetAssetPath(tempObject);
                        path = path.Replace("\\", "/");
                        string[] strs = path.Split('/');
                        string name = strs[strs.Length - 1];
                        name = name.Replace(".prefab", "");
                        selectEffectData.effectName = name;

                        AssetImporter assetImporter = AssetImporter.GetAtPath(path) as AssetImporter;
                        if (assetImporter.assetBundleName == string.Empty)
                        {
                            assetImporter.assetBundleName = "effect";
                        }
                        assetImporter.SaveAndReimport();
                    }
                }
                GUILayout.EndHorizontal();



                GUILayout.BeginHorizontal();

                if (selectEffectData.effectTimeLength == 0f)
                {
                    GUI.color = Color.red;
                }
                else
                {
                    GUI.color = Color.white;
                }

                GUILayout.Label("特效时长:");
                GUI.color = Color.white;
                selectEffectData.effectTimeLength = EditorGUILayout.FloatField(selectEffectData.effectTimeLength);

                GUILayout.Label("特效出现:");
                selectEffectData.castTime = EditorGUILayout.FloatField(selectEffectData.castTime);
                GUILayout.EndHorizontal();

                // 持续性特效和命中特效配置简化   
                if (selectEffectData.effectType == SkillEffectType.Cast ||
                    selectEffectData.effectType == SkillEffectType.PreSkill)
                {

                    GUILayout.Space(10);

                    GUILayout.BeginHorizontal();

                    GUILayout.Label("类型选择");
                    EditorGUI.BeginChangeCheck();
                    int preType = selectEffectData.type;
                    selectEffectData.type = EditorGUILayout.Popup(selectEffectData.type, typeName);

                    if (EditorGUI.EndChangeCheck())
                    {
                        if (preType != selectEffectData.type)
                        {
                            bounsIndex = 0;
                        }
                    }

                    if (selectEffectData.type == 1)
                    {
                        GUILayout.Label("接近过程");
                        if (selectEffectData.curveCloseto == AnimationCurve.Linear(0, 0, 0, 0) || selectEffectData.curveCloseto == null)
                        {
                            selectEffectData.curveCloseto = AnimationCurve.Linear(0, 0, 1f, 1f);
                        }

                        //mselectEffectData.curveCloseto = EditorGUILayout.CurveField(selectEffectData.curveCloseto);
                        GUILayout.Label("追踪玩家");
                        selectEffectData.isFollowPlayer = EditorGUILayout.Toggle(selectEffectData.isFollowPlayer);
                    }


                    if (selectEffectData.type == 3)
                    {
                        GUILayout.BeginHorizontal();
                        GUILayout.Label("飞行过程");
                        if (selectEffectData.curveCloseto == AnimationCurve.Linear(0, 0, 0, 0) || selectEffectData.curveCloseto == null)
                        {
                            selectEffectData.curveCloseto = AnimationCurve.Linear(0, 0, 1f, 1f);
                        }


                        GUILayout.Label("距离");
                        selectEffectData.motionDist = EditorGUILayout.FloatField(selectEffectData.motionDist);
                        GUILayout.EndHorizontal();

                        selectEffectData.curveCloseto = EditorGUILayout.CurveField(selectEffectData.curveCloseto);
                        GUILayout.Label("追踪玩家");
                        //selectEffectData.isFollowPlayer = EditorGUILayout.Toggle(mselectEffectData.isFollowPlayer);

                    }

                    GUILayout.EndHorizontal();

                    if (selectEffectData.type == 1 || selectEffectData.type == 3)
                    {
                        GUILayout.BeginHorizontal();
                        GUILayout.Label("水平方向偏移");
                        selectEffectData.motionOffsetX = EditorGUILayout.FloatField(selectEffectData.motionOffsetX);

                        if (selectEffectData.motionOffsetX != 0)
                        {
                            GUILayout.Label("偏移曲线");
                            if (selectEffectData.curveMotionOffsetX == AnimationCurve.Linear(0, 0, 0, 0) || selectEffectData.curveMotionOffsetX == null)
                            {
                                selectEffectData.curveMotionOffsetX = AnimationCurve.Linear(0, 0, 1f, 1f);
                            }

                            selectEffectData.curveMotionOffsetX = EditorGUILayout.CurveField(selectEffectData.curveMotionOffsetX);
                        }

                        GUILayout.EndHorizontal();

                        GUILayout.BeginHorizontal();
                        GUILayout.Label("垂直方向偏移");
                        selectEffectData.motionOffsetY = EditorGUILayout.FloatField(selectEffectData.motionOffsetY);

                        if (selectEffectData.motionOffsetY != 0)
                        {
                            GUILayout.Label("偏移曲线");
                            if (selectEffectData.curveMotionOffsetY == AnimationCurve.Linear(0, 0, 0, 0) || selectEffectData.curveMotionOffsetY == null)
                            {
                                selectEffectData.curveMotionOffsetY = AnimationCurve.Linear(0, 0, 1f, 1f);
                            }

                            selectEffectData.curveMotionOffsetY = EditorGUILayout.CurveField(selectEffectData.curveMotionOffsetY);
                        }

                        GUILayout.EndHorizontal();
                    }

                    GUILayout.Label("------出现位置------");

                    GUILayout.BeginHorizontal();
                    EditorGUI.BeginChangeCheck();
                    if (selectEffectData.type == 0)
                    {
                        GUILayout.Label("出现的自己的骨骼");
                        bounsIndex = EditorGUILayout.Popup(bounsIndex, root.effectBones.Keys.ToArray());
                        if (EditorGUI.EndChangeCheck())
                        {
                            if (bounsIndex == 0)
                            {
                                selectEffectData.boneName = "";
                            }
                            else
                            {
                                selectEffectData.boneName = root.effectBones.Keys.ToArray()[bounsIndex];
                            }
                        }

                        selectEffectData.isBind = GUILayout.Toggle(selectEffectData.isBind, "是否绑定");
                        if (selectEffectData.isBind)
                        {
                            selectEffectData.isPosition = GUILayout.Toggle(selectEffectData.isPosition, "只绑定位置");
                        }
                    }

                    if (selectEffectData.type == 1) // 追踪型
                    {
                        GUILayout.Label("出现的自己的骨骼");
                        bounsIndex = EditorGUILayout.Popup(bounsIndex, root.effectBones.Keys.ToArray());

                        if (EditorGUI.EndChangeCheck())
                        {
                            if (bounsIndex == 0)
                            {
                                selectEffectData.boneName = "";
                            }
                            else
                            {
                                selectEffectData.boneName = root.targetBoneList.Keys.ToArray()[bounsIndex];
                            }
                        }
                    }

                    GUILayout.EndHorizontal();
                    selectEffectData.offer = EditorGUILayout.Vector3Field("偏移位置", selectEffectData.offer);
                    Vector3 rotation = selectEffectData.rotation.eulerAngles;
                    EditorGUI.BeginChangeCheck();
                    rotation = EditorGUILayout.Vector3Field("旋转角度", rotation);
                    if (EditorGUI.EndChangeCheck())
                    {
                        selectEffectData.rotation = Quaternion.Euler(rotation);
                    }

                }
                else if (selectEffectData.effectType == SkillEffectType.Keep)
                {
                    selectEffectData.type = 0;
                }
                else if (selectEffectData.effectType == SkillEffectType.Hit)
                {
                    selectEffectData.type = 0;

                    GUILayout.Label("------出现位置------");

                    GUILayout.BeginHorizontal();
                    EditorGUI.BeginChangeCheck();
                    GUILayout.Label("出现的自己的骨骼");
                    bounsIndex = EditorGUILayout.Popup(bounsIndex, root.effectBones.Keys.ToArray());
                    if (EditorGUI.EndChangeCheck())
                    {
                        if (bounsIndex == 0)
                        {
                            selectEffectData.boneName = "";
                        }
                        else
                        {
                            selectEffectData.boneName = root.effectBones.Keys.ToArray()[bounsIndex];
                        }
                    }

                    selectEffectData.isBind = GUILayout.Toggle(selectEffectData.isBind, "是否绑定"); ;
                    if (selectEffectData.isBind)
                    {
                        selectEffectData.isPosition = GUILayout.Toggle(selectEffectData.isPosition, "只绑定位置");
                    }

                    GUILayout.EndHorizontal();

                    selectEffectData.offer = EditorGUILayout.Vector3Field("偏移位置", selectEffectData.offer);
                    Vector3 rotation = selectEffectData.rotation.eulerAngles;
                    EditorGUI.BeginChangeCheck();
                    rotation = EditorGUILayout.Vector3Field("旋转角度", rotation);
                    if (EditorGUI.EndChangeCheck())
                    {
                        selectEffectData.rotation = Quaternion.Euler(rotation);
                    }
                }

                GUILayout.Label("音效");
                selectEffectData.sfxName = EditorGUILayout.TextField(selectEffectData.sfxName);

            }
            #endregion

            #region CameraShake
            if (selectCameraData != null)
            {
                // EditorGUI.BeginChangeCheck();
                GUILayout.BeginHorizontal();
                GUILayout.Label("动作类型:");
                selectCameraData.shakeType = EditorGUILayout.Popup(selectCameraData.shakeType, typeCameraMotion);
                GUILayout.EndHorizontal();
                GUILayout.BeginHorizontal();
                GUILayout.Label("出现时间");
                selectCameraData.delayTime = EditorGUILayout.DelayedFloatField(selectCameraData.delayTime);
                GUILayout.EndHorizontal();
                if (selectCameraData.shakeType == 0)  // 抖动
                {
                    GUILayout.BeginHorizontal();
                    GUILayout.Label("震动幅度(5f)");
                    selectCameraData.shakeIntensity = EditorGUILayout.Slider(selectCameraData.shakeIntensity, 0.3f, 10f);
                    GUILayout.EndHorizontal();
                    GUILayout.BeginHorizontal();
                    GUILayout.Label("震动持续时间(0.2f)");
                    selectCameraData.shakeDuration = EditorGUILayout.Slider(selectCameraData.shakeDuration, 0.03f, 1f);
                    GUILayout.EndHorizontal();
                    GUILayout.BeginHorizontal();
                    GUILayout.Label("震动帧数(22)");
                    selectCameraData.shakeFps = (int)EditorGUILayout.Slider(selectCameraData.shakeFps, 10, 29);
                    GUILayout.EndHorizontal();
                }
                else if (selectCameraData.shakeType == 1 || selectCameraData.shakeType == 2)
                {
                    GUILayout.BeginHorizontal();
                    GUILayout.Label("缩放程度(2.5f)");
                    selectCameraData.zoomIntensity = EditorGUILayout.Slider(selectCameraData.zoomIntensity, 0.3f, 10f);
                    GUILayout.EndHorizontal();
                    GUILayout.BeginHorizontal();
                    GUILayout.Label("随机范围(2.0f)");
                    selectCameraData.zoomRandomRate = EditorGUILayout.Slider(selectCameraData.zoomRandomRate, 1f, 3f);
                    GUILayout.EndHorizontal();
                    GUILayout.BeginHorizontal();
                    GUILayout.Label("缩放持续时间(0.25f)");
                    selectCameraData.zoomDuration = EditorGUILayout.Slider(selectCameraData.zoomDuration, 0.03f, 1f);
                    GUILayout.EndHorizontal();
                }
                else if (selectCameraData.shakeType == 3)
                {
                    GUILayout.BeginHorizontal();
                    GUILayout.Label("缩放持续时间(0.25f)");
                    selectCameraData.zoomDuration = EditorGUILayout.Slider(selectCameraData.zoomDuration, 0.03f, 1f);
                    GUILayout.EndHorizontal();
                }
            }
            #endregion
        }
    }
}
